/*处理网络收发的系统指令，文本数据*/

//处理投降、停战议和、强制停战议和(gamedata.type == 0, 广播通道)----------------------------------------
battleMenu.OnSurrend = function(){
	if (campaignRoom.isOnline){
		gameNetService.SendGameData(userSet.user.id, 0, campaign.time_ref.current_step, "gamecmd:surrend", null, null);
	}
};
battleMenu.OnForceSum = function(){
	if (campaignRoom.isOnline){
		gameNetService.SendGameData(userSet.user.id, 0, campaign.time_ref.current_step, "gamecmd:forcesum", null, null);
	}
};
battleMenu.OnSummation = function(){
	if (campaignRoom.isOnline){
		gameNetService.SendGameData(userSet.user.id, 0, campaign.time_ref.current_step, "gamecmd:summation", null, null);

		gettingSummationReply = true;
		gameNetService.GetGameData("", 3, summationReply.HandleGameData);

		SetAllManipulateEnable(false);
		campaignScene.canvaslayer.show(true);
		summationReply.showWaitingUI();
	}
};

var gameBroadcastChannel = {
	HandleGameData : function(gamedata){
		if (gettingSummationReply)
			return;
		
		if (gamedata.param.message == "gamecmd:surrend"){//对方投降			
			gameNetService.StopGetGameData();			
			CloseGameRoomOnline();

			SetAllManipulateEnable(false);
			campaignScene.canvaslayer.show(true);
			battleSum.ShowUI(true);

		}else if (gamedata.param.message == "gamecmd:forcesum"){//对方强制停战议和			
			gameNetService.StopGetGameData();
			CloseGameRoomOnline();

			SetAllManipulateEnable(false);
			campaignScene.canvaslayer.show(true);
			battleSum.ShowUI(true);
		}else if (gamedata.param.message == "gamecmd:summation"){//对方停战议和
			//接收对方是否同意停战的消息（此处只能单一对手）
			saveGetGameData.SaveExist();
			
			gameNetService.StopGetGameData();
			
			SetAllManipulateEnable(false);
			campaignScene.canvaslayer.show(true);
			summationMenu.ShowUI(campaignRoom.users[gamedata.userId].name);
		}
	}
};

//处理同意/拒绝停战议和的消息（gamedata.type == 3, 专用通道）-------------------------------
summationMenu.OnAgreeSummation = function(){
	if (campaignRoom.isOnline){
		gameNetService.SendGameData(userSet.user.id, 3, campaign.time_ref.current_step, "gamecmd:agreesummation", null, null);
	}
	SetAllManipulateEnable(false);
	campaignScene.canvaslayer.show(true);
	battleSum.ShowUI(true);
};

summationMenu.OnRefuseSummation = function(){
	if (campaignRoom.isOnline){
		gameNetService.SendGameData(userSet.user.id, 3, campaign.time_ref.current_step, "gamecmd:refusesummation", null, null);
		saveGetGameData.Restart();
	}
	SetAllManipulateEnable(false);
	campaignScene.canvaslayer.hide(false);
};

var gettingSummationReply = false;

var summationReply = {
		
	HandleGameData : function(gamedata){

		gettingSummationReply = false;
		gameNetService.StopGetGameData();
		summationReply.hideWaitingUI();
		
		if (gamedata.param.message == "gamecmd:agreesummation"){//对方同意停战议和
			
			CloseGameRoomOnline();
				
			SetAllManipulateEnable(false);
			campaignScene.canvaslayer.show(true);
			messageBox.Show("对方同意停战议和", campaignScene.controllayer);
			battleSum.ShowUI(true);

		}else if (gamedata.param.message == "gamecmd:refusesummation"){//对方拒绝停战议和

			SetAllManipulateEnable(true);
			campaignScene.canvaslayer.hide(false);
			messageBox.Show("对方拒绝停战议和", campaignScene.controllayer);
		}
	},	

	//等待label
	_summationWaitingLable : null,
	
	showWaitingUI : function(){
		summationReply._summationWaitingLable = cc.LabelTTF.create("等待对方回应...", "simhei", 64);
		var winSize = cc.director.getWinSize();
		summationReply._summationWaitingLable.attr({x : 0.5 * winSize.width, y : 0.5 * winSize.height});
		campaignScene.canvaslayer.addChild(summationReply._summationWaitingLable);
	},
	
	hideWaitingUI : function(){
		if (summationReply._summationWaitingLable != null){
			summationReply._summationWaitingLable.removeFromParent();
			summationReply._summationWaitingLable = null;
		}
	},
}

var saveGetGameData = {

	//保存并恢复旧的请求数据循环，用于拒绝议和后
	_getGameDataUserid : 0,
	_getGameDataType : 0,
	_getGameDataHandler : null,
	
	SaveExist : function(){
		saveGetGameData._getGameDataUserid = gameNetService._getGameDataUserid;
		saveGetGameData._getGameDataType = gameNetService._getGameDataType;
		saveGetGameData._getGameDataHandler = gameNetService._getGameDataHandler;
	},
	Restart : function(){
		gameNetService.GetGameData(saveGetGameData._getGameDataUserid, saveGetGameData._getGameDataType, saveGetGameData._getGameDataHandler);
	},
};

//关闭游戏房间---------------------------------------------------------------
var CloseGameRoomOnline = function(){
	if (campaignRoom.isOnline)
		netService.CloseRoom(userSet.user.id, campaignRoom.id, "", null, 0);
};